<template>
  <!-- 后期处理-使用UnrealBloomPass通道在场景中添加泛光效果，三维物体表面发光效果 -->
  <div id="scene" />
</template>

<script>
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js";
import { UnrealBloomPass } from "three/examples/jsm/postprocessing/UnrealBloomPass.js";

let scene;
let camera;
let renderer;
let controls;

let composer; //效果组合器
let unrealBloomPass; //UnrealBloomPass泛光通道
export default {
  data() {
    return {};
  },
  mounted() {
    this.init();
  },
  methods: {
    // 初始化
    init() {
      this.createScene(); // 创建场景
      this.createModels(); // 创建模型
      this.createLight(); // 创建光源
      this.createCamera(); // 创建相机
      this.createRender(); // 创建渲染器
      this.createControls(); // 创建控件对象
      this.createComposer();
      this.render(); // 渲染
    },
    // 创建场景
    createScene() {
      scene = new THREE.Scene();
    },
    // 创建模型
    createModels() {
      const cubeGeometry = new THREE.BoxGeometry(3, 3, 3);
      const material = new THREE.MeshLambertMaterial({
        color: 0x00ff00,
      });

      const cube = new THREE.Mesh(cubeGeometry, material);
      cube.position.set(-5, 0, 0);
      scene.add(cube);

      const sphereGeometry = new THREE.SphereGeometry(2, 50, 50);

      const sphere = new THREE.Mesh(sphereGeometry, material);
      sphere.position.set(1, 0, 0);
      scene.add(sphere);
    },
    // 创建光源
    createLight() {
      // 环境光
      const ambientLight = new THREE.AmbientLight(0xffffff, 0.3); // 创建环境光
      scene.add(ambientLight); // 将环境光添加到场景

      const directionLight = new THREE.DirectionalLight(0xffffff);
      directionLight.position.set(550, 100, 550);
      directionLight.intensity = 0.8;
      scene.add(directionLight);
    },
    // 创建相机
    createCamera() {
      const element = document.getElementById("scene");
      camera = new THREE.PerspectiveCamera(
        45,
        element.clientWidth / element.clientHeight,
        0.1,
        1000
      );
      camera.position.set(0, 0, 30); // 设置相机位置

      camera.lookAt(new THREE.Vector3(0, 0, 0)); // 设置相机方向
    },
    // 创建渲染器
    createRender() {
      const element = document.getElementById("scene");
      renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
      renderer.setSize(element.clientWidth, element.clientHeight); // 设置渲染区域尺寸
      renderer.shadowMap.enabled = true; // 显示阴影
      renderer.setClearColor(0x000000, 1); // 设置背景颜色
      element.appendChild(renderer.domElement);
    },
    createComposer() {
      //使用场景和相机创建RenderPass通道
      const renderPass = new RenderPass(scene, camera);

      //创建UnrealBloomPass泛光通道
      unrealBloomPass = new UnrealBloomPass(
        new THREE.Vector2(256, 256),
        1,
        1.1,
        0.18
      );
      unrealBloomPass.renderToScreen = true;
      //创建效果组合器
      composer = new EffectComposer(renderer);

      //将创建的通道添加到EffectComposer(效果组合器)对象中
      composer.addPass(renderPass);
      composer.addPass(unrealBloomPass);
    },
    render() {
      renderer.render(scene, camera);

      /********** 更新效果组合器一定要在渲染器更新后，否则通道无法产生效果************/
      composer.render(); //效果组合器更新
      requestAnimationFrame(this.render);
    },
    // 创建控件对象
    createControls() {
      controls = new OrbitControls(camera, renderer.domElement);
    },
  },
};
</script>

<style>
#scene {
  width: 100%;
  height: 100%;
}
</style>

